<?php
class KetquaController extends Controller{
    public function actionIndex(){
        $this->actionTruyenthong();
    }

    public function actionTruyenthong(){ 
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? $_GET["page"]:1;
        $begin = isset($_GET["begin"]) ? $_GET["begin"]:"";
        if($begin !=""){
            $arr_begin = explode("/",$begin);
            $begin_format = $arr_begin[2]."-".$arr_begin[1]."-".$arr_begin[0];
        }else{
            $begin_format = "";
        }

        $end = isset($_GET["end"]) ? $_GET["end"]:"";
        if($end !=""){
            $arr_end = explode("/",$end);
            $end_format = $arr_end[2]."-".$arr_end[1]."-".$arr_end[0];
        }else{
            $end_format = "";
        }
        $count = AdminKqMienbac::getCount($begin_format,$end_format);

        $row_count = floor ( $count ["count"] / $row_per_page );
        $row_count += $count ["count"] % $row_per_page == 0 ? 0 : 1;
        $limitFirst = ($page - 1) * $row_per_page;

        $rows = AdminKqMienbac::getRowInDate($limitFirst,$row_per_page,$begin_format,$end_format);

        $this->render("truyenthong",array("rows"=>$rows,"page"=>$page,"row_count"=>$row_count,"begin"=>$begin,"end"=>$end));
    }

    public function actionCreateTruyenthong(){
        $ngay_quay = isset($_GET["ngay_quay"]) && $_GET["ngay_quay"] != ""  ? $_GET["ngay_quay"]:date("d-m-Y",time());
        $ngay_quay = str_replace("-","/",$ngay_quay);
        $arr_ngay = explode("/",$ngay_quay);
        if(count($arr_ngay) >2){
            $date = $arr_ngay[2]."-".$arr_ngay[1]."-".$arr_ngay[0];
        }
        $row = AdminKqMienbac::getRowByDate($date);
        $this->render("truyenthong_create",array("row"=>$row,"ngay_quay"=>$ngay_quay));
    }

    public function actionTruyenthongDetail(){

        $id = isset($_GET["id"]) ? $_GET["id"]:"";   
        $row = AdminKqMienbac::getRowById($id);
        $this->render("truyenthong_detail",array("row"=>$row));
    }

    public function actionEditTruyenthong(){
        $id = isset($_GET["id"]) ? $_GET["id"]:"";
        $row = AdminKqMienbac::getRowById($id);
        $this->render("truyenthong_edit",array("row"=>$row));
    }

    public function actionDeleteTruyenthong(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):""; 
        $result = AdminKqMienbac::deleteRowById($id);
        echo $result;
    }


    public function actionMiennam(){
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? $_GET["page"]:1;
        $begin = isset($_GET["begin"]) ? $_GET["begin"]:"";
        if($begin !=""){
            $arr_begin = explode("/",$begin);
            $begin_format = $arr_begin[2]."-".$arr_begin[1]."-".$arr_begin[0];
        }else{
            $begin_format = "";
        }

        $end = isset($_GET["end"]) ? $_GET["end"]:"";
        if($end !=""){
            $arr_end = explode("/",$end);
            $end_format = $arr_end[2]."-".$arr_end[1]."-".$arr_end[0];
        }else{
            $end_format = "";
        }
        $id = isset($_GET["id"]) ? $_GET["id"]:"";

        $count = AdminKqMiennam::getCountByProvince($id,$begin_format,$end_format);

        $row_count = floor ( $count ["count"] / $row_per_page );
        $row_count += $count ["count"] % $row_per_page == 0 ? 0 : 1;
        $limitFirst = ($page - 1) * $row_per_page;

        $rowProvince = AdminProvince::getRowById($id);

        $rows = AdminKqMiennam::getRowInDateAndProvince($id,$limitFirst,$row_per_page,$begin_format,$end_format);

        $this->render("miennam",array("rows"=>$rows,"rowProvince"=>$rowProvince,"page"=>$page,"row_count"=>$row_count,"begin"=>$begin,"end"=>$end));
    }

    public function actionCreateMiennam(){

        $ngay_quay = isset($_GET["ngay_quay"]) && $_GET["ngay_quay"] != ""  ? $_GET["ngay_quay"]:date("d-m-Y",time());
        
        $ngay_quay = str_replace("-","/",$ngay_quay);
        $arr_ngay = explode("/",$ngay_quay);
        if(count($arr_ngay) >2){
            $time = mktime(0,0,0,$arr_ngay[1],$arr_ngay[0],$arr_ngay[2]);
            $date = $arr_ngay[2]."-".$arr_ngay[1]."-".$arr_ngay[0];
        }
        $row = array();
        $day = getdate($time);
        $wday = $day["wday"]; 
        $day_of_week = StringUtils::getDayVi($wday);  
        $mien_nam = ConstantsUtil::MIEN_NAM; 
        $rowProvince = AdminProvince::model()->getRowsByDateAndRegion($mien_nam,$day_of_week);
        for($i=0;$i<count($rowProvince);$i++){
            $rows[$i] = AdminKqMiennam::getRowByDateAndProvince($rowProvince[$i]["id"],$date);                 
        }
        $this->render("miennam_create",array("rows"=>$rows,"day"=>$day,"rowProvince"=>$rowProvince,"ngay_quay"=>$ngay_quay));
    }

    public function actionMiennamDetail(){
        /*$date = isset($_GET["date"]) ? $_GET["date"]:"";
        if($date !=""){
        $arr_date = explode("/",$date);
        $date = $arr_date[2]."-".$arr_date[1]."-".$arr_date[0];
        } */  
        $id = isset($_GET["id"]) ? $_GET["id"]:"";   
        $row = AdminKqMiennam::getRowById($id);
        $rowProvince = AdminProvince::getRowById($row["province_id"]);
        $this->render("miennam_detail",array("row"=>$row,"rowProvince"=>$rowProvince));
    }

    public function actionEditMiennam(){
        $id = isset($_GET["id"]) ? $_GET["id"]:"";
        $row = AdminKqMiennam::getRowById($id);
        $rowProvince = AdminProvince::getRowById($row["province_id"]);
        $this->render("miennam_edit",array("row"=>$row,"rowProvince"=>$rowProvince));
    }

    public function actionDeleteMiennam(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):""; 
        $result = AdminKqMiennam::deleteRowById($id);
        echo $result; 
    }

    public function actionMientrung(){
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? $_GET["page"]:1;
        $begin = isset($_GET["begin"]) ? $_GET["begin"]:"";
        if($begin !=""){
            $arr_begin = explode("/",$begin);
            $begin_format = $arr_begin[2]."-".$arr_begin[1]."-".$arr_begin[0];
        }else{
            $begin_format = "";
        }

        $end = isset($_GET["end"]) ? $_GET["end"]:"";
        if($end !=""){
            $arr_end = explode("/",$end);
            $end_format = $arr_end[2]."-".$arr_end[1]."-".$arr_end[0];
        }else{
            $end_format = "";
        }
        $id = isset($_GET["id"]) ? $_GET["id"]:"";

        $count = AdminKqMientrung::getCountByProvince($id,$begin_format,$end_format);
        $row_count = floor ( $count ["count"] / $row_per_page );
        $row_count += $count ["count"] % $row_per_page == 0 ? 0 : 1;
        $limitFirst = ($page - 1) * $row_per_page;

        $rowProvince = AdminProvince::getRowById($id);

        $rows = AdminKqMientrung::getRowInDateAndProvince($id,$limitFirst,$row_per_page,$begin_format,$end_format);

        $this->render("mientrung",array("rows"=>$rows,"rowProvince"=>$rowProvince,"page"=>$page,"row_count"=>$row_count,"begin"=>$begin,"end"=>$end));
    }

    public function actionCreateMientrung(){

        $ngay_quay = isset($_GET["ngay_quay"]) && $_GET["ngay_quay"] != ""  ? $_GET["ngay_quay"]:date("d-m-Y",time());
        $ngay_quay = str_replace("-","/",$ngay_quay);
        $arr_ngay = explode("/",$ngay_quay);
        if(count($arr_ngay) >2){
            $time = mktime(0,0,0,$arr_ngay[1],$arr_ngay[0],$arr_ngay[2]);
            $date = $arr_ngay[2]."-".$arr_ngay[1]."-".$arr_ngay[0];
        }
        $row = array();
        $day = getdate($time);
        $wday = $day["wday"]; 
        $day_of_week = StringUtils::getDayVi($wday);  
        $mien_trung = ConstantsUtil::MIEN_TRUNG; 
        $rowProvince = AdminProvince::model()->getRowsByDateAndRegion($mien_trung,$day_of_week);
        for($i=0;$i<count($rowProvince);$i++){
            $rows[$i] = AdminKqMientrung::getRowByDateAndProvince($rowProvince[$i]["id"],$date);                 
        }
        $this->render("mientrung_create",array("rows"=>$rows,"day"=>$day,"rowProvince"=>$rowProvince,"ngay_quay"=>$ngay_quay));
    }

    public function actionMientrungDetail(){
        /*$date = isset($_GET["date"]) ? $_GET["date"]:"";
        if($date !=""){
        $arr_date = explode("/",$date);
        $date = $arr_date[2]."-".$arr_date[1]."-".$arr_date[0];
        } */  
        $id = isset($_GET["id"]) ? $_GET["id"]:"";   
        $row = AdminKqMientrung::getRowById($id);
        $rowProvince = AdminProvince::getRowById($row["province_id"]);
        $this->render("mientrung_detail",array("row"=>$row,"rowProvince"=>$rowProvince));
    }

    public function actionEditMientrung(){
        $id = isset($_GET["id"]) ? $_GET["id"]:"";
        $row = AdminKqMientrung::getRowById($id);
        $rowProvince = AdminProvince::getRowById($row["province_id"]);
        $this->render("mientrung_edit",array("row"=>$row,"rowProvince"=>$rowProvince));
    }

    public function actionDeleteMientrung(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):""; 
        $result = AdminKqMientrung::deleteRowById($id);
        echo $result;
    }

    public function actionDientoan123(){
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? $_GET["page"]:1;
        $begin = isset($_GET["begin"]) ? $_GET["begin"]:"";
        if($begin !=""){
            $arr_begin = explode("/",$begin);
            $begin_format = $arr_begin[2]."-".$arr_begin[1]."-".$arr_begin[0];
        }else{
            $begin_format = "";
        }

        $end = isset($_GET["end"]) ? $_GET["end"]:"";
        if($end !=""){
            $arr_end = explode("/",$end);
            $end_format = $arr_end[2]."-".$arr_end[1]."-".$arr_end[0];
        }else{
            $end_format = "";
        }

        $count = AdminDientoan123::getCount($begin_format,$end_format);
        $row_count = floor ( $count ["count"] / $row_per_page );
        $row_count += $count ["count"] % $row_per_page == 0 ? 0 : 1;
        $limitFirst = ($page - 1) * $row_per_page;

        $rows = AdminDientoan123::getRowInDate($limitFirst,$row_per_page,$begin_format,$end_format);

        $this->render("dientoan123",array("rows"=>$rows,"page"=>$page,"row_count"=>$row_count,"begin"=>$begin,"end"=>$end));

    }
    public function actionCreateDientoan123(){

       $ngay_quay = isset($_GET["ngay_quay"]) && $_GET["ngay_quay"] != ""  ? $_GET["ngay_quay"]:date("d-m-Y",time());
        $ngay_quay = str_replace("-","/",$ngay_quay);
        $arr_ngay = explode("/",$ngay_quay);
        if(count($arr_ngay) >2){
            $time = mktime(0,0,0,$arr_ngay[1],$arr_ngay[0],$arr_ngay[2]);
            $date = $arr_ngay[2]."-".$arr_ngay[1]."-".$arr_ngay[0];
        }
        $row = AdminDientoan123::getRowByDate($date);
        
        $this->render("create_dientoan123",array("row"=>$row,"ngay_quay"=>$ngay_quay));
    }

    public function actionEditDientoan123(){
        $id = isset($_GET["id"]) ? $_GET["id"]:"";
        $row = AdminDientoan123::getRowById($id);

        $this->render("dientoan123_edit",array("row"=>$row));
    }

    public function actionDeleteDientoan123(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):""; 
        $result = AdminDientoan123::deleteRowById($id);
        echo $result; 
    }

    public function actionDientoan6x36(){
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? $_GET["page"]:1;
        $begin = isset($_GET["begin"]) ? $_GET["begin"]:"";
        if($begin !=""){
            $arr_begin = explode("/",$begin);
            $begin_format = $arr_begin[2]."-".$arr_begin[1]."-".$arr_begin[0];
        }else{
            $begin_format = "";
        }

        $end = isset($_GET["end"]) ? $_GET["end"]:"";
        if($end !=""){
            $arr_end = explode("/",$end);
            $end_format = $arr_end[2]."-".$arr_end[1]."-".$arr_end[0];
        }else{
            $end_format = "";
        }

        $count = AdminDientoan6x36::getCount($begin_format,$end_format);
        $row_count = floor ( $count ["count"] / $row_per_page );
        $row_count += $count ["count"] % $row_per_page == 0 ? 0 : 1;
        $limitFirst = ($page - 1) * $row_per_page;

        $rows = AdminDientoan6x36::getRowInDate($limitFirst,$row_per_page,$begin_format,$end_format);

        $this->render("dientoan6x36",array("rows"=>$rows,"page"=>$page,"row_count"=>$row_count,"begin"=>$begin,"end"=>$end));

    }
    public function actionCreateDientoan6x36(){

        $ngay_quay = isset($_GET["ngay_quay"]) && $_GET["ngay_quay"] != ""  ? $_GET["ngay_quay"]:date("d-m-Y",time());
        $ngay_quay = str_replace("-","/",$ngay_quay);
        $arr_ngay = explode("/",$ngay_quay);
        if(count($arr_ngay) >2){
            $time = mktime(0,0,0,$arr_ngay[1],$arr_ngay[0],$arr_ngay[2]);
            $date = $arr_ngay[2]."-".$arr_ngay[1]."-".$arr_ngay[0];
        }
        $row = AdminDientoan6x36::getRowByDate($date);
        
        $this->render("create_dientoan6x36",array("row"=>$row,"ngay_quay"=>$ngay_quay));
    }

    public function actionEditDientoan6x36(){
        $id = isset($_GET["id"]) ? $_GET["id"]:"";
        $row = AdminDientoan6x36::getRowById($id);

        $this->render("dientoan6x36_edit",array("row"=>$row));
    }

    public function actionDeleteDientoan6x36(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):""; 
        $result = AdminDientoan6x36::deleteRowById($id);
        echo $result; 
    }

    public function actionThantai(){
        $row_per_page = 10;
        $page = isset($_GET["page"]) ? $_GET["page"]:1;
        $begin = isset($_GET["begin"]) ? $_GET["begin"]:"";
        if($begin !=""){
            $arr_begin = explode("/",$begin);
            $begin_format = $arr_begin[2]."-".$arr_begin[1]."-".$arr_begin[0];
        }else{
            $begin_format = "";
        }

        $end = isset($_GET["end"]) ? $_GET["end"]:"";
        if($end !=""){
            $arr_end = explode("/",$end);
            $end_format = $arr_end[2]."-".$arr_end[1]."-".$arr_end[0];
        }else{
            $end_format = "";
        }

        $count = AdminThantai::getCount($begin_format,$end_format);
        $row_count = floor ( $count ["count"] / $row_per_page );
        $row_count += $count ["count"] % $row_per_page == 0 ? 0 : 1;
        $limitFirst = ($page - 1) * $row_per_page;

        $rows = AdminThantai::getRowInDate($limitFirst,$row_per_page,$begin_format,$end_format);

        $this->render("thantai",array("rows"=>$rows,"page"=>$page,"row_count"=>$row_count,"begin"=>$begin,"end"=>$end));

    }
    
    public function actionCreateThantai(){

        $ngay_quay = isset($_GET["ngay_quay"]) && $_GET["ngay_quay"] != ""  ? $_GET["ngay_quay"]:date("d-m-Y",time());
        $ngay_quay = str_replace("-","/",$ngay_quay);
        $arr_ngay = explode("/",$ngay_quay);
        if(count($arr_ngay) >2){
            $time = mktime(0,0,0,$arr_ngay[1],$arr_ngay[0],$arr_ngay[2]);
            $date = $arr_ngay[2]."-".$arr_ngay[1]."-".$arr_ngay[0];
        }
        $row = AdminThantai::getRowByDate($date);
        
        $this->render("create_thantai",array("row"=>$row,"ngay_quay"=>$ngay_quay));
    }

    public function actionEditThantai(){
        $id = isset($_GET["id"]) ? $_GET["id"]:"";
        $row = AdminThantai::getRowById($id);

        $this->render("thantai_edit",array("row"=>$row));
    }

    public function actionDeleteThantai(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):""; 
        $result = AdminThantai::deleteRowById($id);
        echo $result; 
    }
}
